package com.wise.service.cms.content.product;

import com.wise.entities.content.Product;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoOne;
import com.wise.framework.daomodel.DaoOne4Column;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.session.CurrentUserId;
import com.wise.framework.validation.Precondition;
import com.wise.summer.v1.cms.content.product.CopyProduct;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.Objects;

import static com.wise.model.content.ContentErrMsgs.Err_001000250009;
import static com.wise.model.content.ContentErrMsgs.Err_001000250032;

/**
 * @author Pitaya
 * @date 2019-11-13
 */

@Service
@SummerService
public class CopyProductService implements SummerServiceBean<CopyProduct> {
    @Override
    public void sum(CopyProduct summer) {
        Product product = DaoOne.s(Product.class, summer.getId(), Err_001000250009);
        Product prod = DaoOne4Column.s(Product.class, Product._routeId, summer.getRouteId(), null);
        Precondition.checkState(Objects.isNull(prod), Err_001000250032);

        Product pr = new Product();

        BeanUtils.copyProperties(product, pr, Product._id);
        pr.setName(summer.getName());
        pr.setRouteId(summer.getRouteId());
        pr.setCreationDate(DateTimeUtil.timestampNow());
        pr.setCreatedBy(CurrentUserId.s(true));
        pr.setLastUpdateBy(null);
        pr.setLastUpdateDate(null);

        DaoSave.s(pr);

        summer.setSummerResult(true);
    }
}
